package com.ruoyi.web.controller.news;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.news.NewsImportantDynamic;
import com.ruoyi.system.domain.news.dto.NewsImportantDynamicDto;
import com.ruoyi.system.domain.news.query.NewsImportantDynamicQuery;
import com.ruoyi.system.domain.news.vo.NewsImportantDynamicVo;
import com.ruoyi.system.service.news.NewsImportantDynamicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/news/newsImportantDynamic")
@Api(value = "newsImportantDynamicController", tags  = "要闻动态")
public class NewsImportantDynamicController extends BaseController {

	@Resource
	private NewsImportantDynamicService newsImportantDynamicService;

    @ApiOperation(value = "新增/修改要闻动态信息", notes = "新增/修改要闻动态信息")
	@PostMapping("/submit")
	public R submit(@RequestBody NewsImportantDynamicDto newsImportantDynamicDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		NewsImportantDynamic newsImportantDynamic = new NewsImportantDynamic();
		BeanUtils.copyProperties(newsImportantDynamicDto,newsImportantDynamic);
		if (newsImportantDynamic.getId()==null) {
			newsImportantDynamic.setCreateBy(loginUser.getUsername());
			newsImportantDynamic.setDeptId(loginUser.getDeptId());
			newsImportantDynamic.setCreateTime(new Date());
			newsImportantDynamic.setUpdateBy(loginUser.getUsername());
			newsImportantDynamic.setUpdateTime(new Date());
		} else {
			newsImportantDynamic.setUpdateBy(loginUser.getUsername());
			newsImportantDynamic.setUpdateTime(new Date());
		}
		boolean result = newsImportantDynamicService.saveOrUpdate(newsImportantDynamic);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "删除要闻动态信息", notes = "删除要闻动态信息")
	@DeleteMapping("/delete")
	public R delete(@RequestParam(value = "id") Long id) {
		boolean result = newsImportantDynamicService.removeById(id);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "获取单个要闻动态信息", notes = "获取单个要闻动态信息")
	@GetMapping("/getById")
	public R<NewsImportantDynamicVo> getById(@RequestParam(value = "id") Long id) {
		NewsImportantDynamicQuery newsImportantDynamicQuery = new NewsImportantDynamicQuery();
		newsImportantDynamicQuery.setId(id);
		List<NewsImportantDynamicVo> newsImportantDynamicList = newsImportantDynamicService.getNewsImportantDynamicList(newsImportantDynamicQuery);
		if (newsImportantDynamicList!=null&&newsImportantDynamicList.size()>0) {
			return R.ok(newsImportantDynamicList.get(0));
		} else {
			return R.fail("数据不存在");
		}
	}

	@ApiOperation(value = "获取要闻动态信息列表")
	@PostMapping(value = "/list")
	public R<PageInfo<NewsImportantDynamicVo>> list(@RequestBody NewsImportantDynamicQuery newsImportantDynamicQuery) {
		List<NewsImportantDynamicVo> newsImportantDynamicList = newsImportantDynamicService.getNewsImportantDynamicList(newsImportantDynamicQuery);
		return R.ok(new PageInfo<>(newsImportantDynamicList));
	}
}
